package com.controller;

import com.api.CommonResult;
import com.entity.HomePage;
import com.entity.table.BZpxsTableDef;
import com.entity.table.HomePageTableDef;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.entity.BZpxs;
import com.service.BZpxsService;

import java.io.Serializable;
import java.util.List;

/**
 *  控制层。
 *
 * @author admin
 * @since 2024-06-17
 */
@Controller
@RequestMapping("/admin/BZpxs")
public class BZpxsController {

    @Autowired
    private BZpxsService bZpxsService;

    /**
     * 添加。
     *
     * @param bZpxs 
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
//    @PostMapping("save")
//    public boolean save(@RequestBody BZpxs bZpxs) {
//        return bZpxsService.save(bZpxs);
//    }

    /**
     * 根据主键删除。
     *
     * @param id 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @DeleteMapping("remove/{id}")
    public boolean remove(@PathVariable Serializable id) {
        return bZpxsService.removeById(id);
    }

    /**
     * 根据主键更新。
     *
     * @param bZpxs 
     * @return {@code true} 更新成功，{@code false} 更新失败
     */
//    @PutMapping("update")
//    public boolean update(@RequestBody BZpxs bZpxs) {
//        return bZpxsService.updateById(bZpxs);
//    }

    /**
     * 查询所有。
     *
     * @return 所有数据
     */
    @GetMapping("list")
    public List<BZpxs> list() {
        return bZpxsService.list();
    }

    /**
     * 根据主键获取详细信息。
     *
     * @param id 主键
     * @return 详情
     */
    @GetMapping("getInfo/{id}")
    public BZpxs getInfo(@PathVariable Serializable id) {
        return bZpxsService.getById(id);
    }

    /**
     * 分页查询。
     *
     * @param page 分页对象
     * @return 分页对象
     */
    @GetMapping("page")
    public Page<BZpxs> page(Page<BZpxs> page) {
        return bZpxsService.page(page);
    }

    @PostMapping("save")
    @ResponseBody
    public CommonResult save(BZpxs bZpxs) {
        try{
            boolean r = bZpxsService.save(bZpxs);
            return CommonResult.success(r);
        }catch (Exception e){
            e.printStackTrace();
            String msg = bZpxs.getTitle()+"已存在";
            return CommonResult.failed(msg);
        }

    }
    @PostMapping("update")
    @ResponseBody
    public CommonResult update(BZpxs bZpxs) {
        try{
            boolean r = bZpxsService.updateById(bZpxs);
            return CommonResult.success(r);
        } catch (Exception e) {
            e.printStackTrace();
            String msg = bZpxs.getTitle()+"已存在";
            return CommonResult.failed(msg);
        }
    }
    @GetMapping("Ccc")
    public String typeList(ModelMap model, @RequestParam(value = "bZpxsTitle",defaultValue = "") String bZpxsTitle,
                           @RequestParam(value = "p", defaultValue = "1") int p,
                           @RequestParam(value = "pageSize", defaultValue = "10")int pageSize) {
        QueryWrapper queryWrapper = QueryWrapper.create().where(BZpxsTableDef.B_ZPXS.TITLE.like(bZpxsTitle));
        Page<BZpxs> page = new Page<>();
        page.setPageSize(pageSize);
        page.setPageNumber(p);
        Page<BZpxs> pageInfo = bZpxsService.page(page,queryWrapper);
        model.addAttribute("page", pageInfo);
        model.addAttribute("total", pageInfo.getTotalRow());
        return "/back/bZpxsTitle-list";
    }
    @GetMapping(value = "/addView")
    public  String addUserView(){ return "back/bZpxsTitle-add";}

    @GetMapping(value = "updateView")
    public String updateView(int id,ModelMap model){
        BZpxs bZpxs = bZpxsService.getById(id);
        model.addAttribute("bZpxs",bZpxs);
        return "/back/bZpxsTitle-edit";
    }
    @PostMapping("del")
    @ResponseBody
    public CommonResult update(Integer id) {
        try {
            boolean r = bZpxsService.removeById(id);
            return CommonResult.success(r);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed(e.getMessage());
        }
    }
}
